package classfile

const (
	MAGIC = 0xCAFEBABE
)

func (self *ClassFile) Read(reader *ClassReader) {
	self.ReadAndCheckMagic(reader)
	self.ReadAndCheckVersion(reader)
	self.Constantpool = readConstantPool(reader)
	self.AccessFlags = reader.ReadUint16()
	self.ThisClass = reader.ReadUint16()
	self.SuperClass = reader.ReadUint16()
	self.Interfaces = reader.ReadUint16s()
	self.Fields = readMembers(reader, self.Constantpool)
	self.Methods = readMembers(reader, self.Constantpool)
	self.Attributes = readAttributes(reader, self.Constantpool)
}

func (self *ClassFile) ReadAndCheckMagic(reader *ClassReader) {
	magic := reader.ReadUint32()
	if magic != MAGIC {
		panic("java.lang.ClassFormatError:magic!")
	}
}

func (self *ClassFile) ReadAndCheckVersion(reader *ClassReader) {
	self.MinorVersion = reader.ReadUint16()
	self.MajorVersion = reader.ReadUint16()
	switch self.MajorVersion {
	case 45:
		return
	case 46,47,48,49,50,51,52:
		{
			if self.MinorVersion == 0 {
				return
			}
		}
	}
	panic("java.lang.UnsupportedClassVersionError!")
}
